<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>使用用户自定义函数对数组中的每个元素做回调处理</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.array-walk-recursive.html">« array_walk_recursive</a></li>
      <li style="float: right;"><a href="function.array.html">array »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.array.html">数组 函数</a></li>
    <li>使用用户自定义函数对数组中的每个元素做回调处理</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.array-walk" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">array_walk</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">array_walk</span> &mdash; <span class="dc-title">使用用户自定义函数对数组中的每个元素做回调处理</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.array-walk-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array_walk</strong></span>(<span class="methodparam"><span class="type"><span class="type">array</span>|<span class="type">object</span></span> <code class="parameter reference">&$array</code></span>, <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$arg</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type">bool</span></div>

  <p class="simpara">
   将用户自定义函数 <code class="parameter">callback</code> 应用到
   <code class="parameter">array</code> 数组中的每个单元。
  </p>
  <p class="para">
   <span class="function"><strong>array_walk()</strong></span> 不会受到 <code class="parameter">array</code>
   内部数组指针的影响。<span class="function"><strong>array_walk()</strong></span>
   会遍历整个数组而不管指针的位置。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.array-walk-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">array</code></dt>

     <dd>

      <p class="para">
       输入的数组。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">callback</code></dt>

     <dd>

      <p class="para">
       典型情况下
   <code class="parameter">callback</code> 接受两个参数。<code class="parameter">array</code>
   参数的值作为第一个，键名作为第二个。
      </p>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <p class="para">
       如果 <code class="parameter">callback</code> 需要直接作用于数组中的值，则给
    <code class="parameter">callback</code> 的第一个参数指定为<a href="language.references.html" class="link">引用</a>。这样任何对这些单元的改变也将会改变原始数组本身。
       </p>
      </p></blockquote>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
        <p class="para">
         参数数量超过预期，传入内置函数 (例如 <span class="function"><a href="function.strtolower.html" class="function">strtolower()</a></span>)，
         将抛出警告，所以不适合当做 <code class="parameter">callback</code>。
        </p>
      </p></blockquote>
      <p class="para">
       只有 <code class="parameter">array</code> 的值才可以被改变，用户不应在回调函数中改变该数组本身的结构。例如增加/删除单元，unset
   单元等等。如果 <span class="function"><strong>array_walk()</strong></span>
   作用的数组改变了，则此函数的的行为未经定义，且不可预期。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">arg</code></dt>

     <dd>

      <p class="para">
       如果提供了可选参数 <code class="parameter">arg</code>，将被作为第三个参数传递给
       <code class="parameter">callback</code>。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.array-walk-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回 <strong><code>true</code></strong>。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.array-walk-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   从 PHP 7.1.0 起，如果 <code class="parameter">callback</code> 函数需要的参数（数组成员的值和键）超过2个或者
   也传递了 <code class="parameter">arg</code> 参数需要的参数超过3个时，会抛出 <span class="classname"><a href="class.argumentcounterror.html" class="classname">ArgumentCountError</a></span>。
   在此之前，每次 <span class="function"><strong>array_walk()</strong></span> 调用 <code class="parameter">callback</code>
   的时候都会生成 <a href="errorfunc.constants.html" class="link">E_WARNING</a> 级别的错误。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.array-walk-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       如果 <code class="parameter">callback</code> 期待通过引用传递第二个和第三个参数，
       此函数现在将抛出 <strong><code>E_WARNING</code></strong>。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.array-walk-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4971">
    <p><strong>示例 #1 <span class="function"><strong>array_walk()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$fruits&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"d"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"lemon"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"orange"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"b"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"banana"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"c"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"apple"</span><span style="color: #007700">);<br /><br />function&nbsp;</span><span style="color: #0000BB">test_alter</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$item1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$prefix</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$item1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$prefix</span><span style="color: #DD0000">:&nbsp;</span><span style="color: #0000BB">$item1</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />}<br /><br />function&nbsp;</span><span style="color: #0000BB">test_print</span><span style="color: #007700">(</span><span style="color: #0000BB">$item2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$key</span><span style="color: #DD0000">.&nbsp;</span><span style="color: #0000BB">$item2</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Before&nbsp;...:\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">array_walk</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruits</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'test_print'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">array_walk</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruits</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'test_alter'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"...&nbsp;and&nbsp;after:\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">array_walk</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruits</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'test_print'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Before ...:
d. lemon
a. orange
b. banana
c. apple
... and after:
d. fruit: lemon
a. fruit: orange
b. fruit: banana
c. fruit: apple
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4972">
    <p><strong>示例 #2 <span class="function"><strong>array_walk()</strong></span> example using anonymous function</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$elements&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">'a'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'b'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'c'</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">array_walk</span><span style="color: #007700">(</span><span style="color: #0000BB">$elements</span><span style="color: #007700">,&nbsp;function&nbsp;(</span><span style="color: #0000BB">$value</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #007700">{</span><span style="color: #0000BB">$key</span><span style="color: #007700">}</span><span style="color: #DD0000">&nbsp;=&gt;&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$value</span><span style="color: #007700">}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />});<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
0 =&gt; a
1 =&gt; b
2 =&gt; c
</pre></div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.array-walk-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.array-walk-recursive.html" class="function" rel="rdfs-seeAlso">array_walk_recursive()</a> - 对数组中的每个成员递归地应用用户函数</span></li>
    <li class="member"><span class="function"><a href="function.iterator-apply.html" class="function" rel="rdfs-seeAlso">iterator_apply()</a> - 为迭代器中每个元素调用函数</span></li>
    <li class="member"><span class="function"><a href="function.list.html" class="function" rel="rdfs-seeAlso">list()</a> - 把数组中的值赋给一组变量</span></li>
    <li class="member"><span class="function"><a href="function.each.html" class="function" rel="rdfs-seeAlso">each()</a> - 返回数组中当前的键／值对并将数组指针向前移动一步</span></li>
    <li class="member"><span class="function"><a href="function.call-user-func-array.html" class="function" rel="rdfs-seeAlso">call_user_func_array()</a> - 调用回调函数，并把一个数组参数作为回调函数的参数</span></li>
    <li class="member"><span class="function"><a href="function.array-map.html" class="function" rel="rdfs-seeAlso">array_map()</a> - 为数组的每个元素应用回调函数</span></li>
    <li class="member"><a href="control-structures.foreach.html" class="link">foreach</a></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>